package com.nytd.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nytd.demo.common.constant.Constant;
import com.nytd.demo.common.exception.BlogException;
import com.nytd.demo.mapper.BlogInfoMapper;
import com.nytd.demo.pojo.dataobject.BlogInfo;
import com.nytd.demo.pojo.request.AddBlogRequest;
import com.nytd.demo.pojo.request.UpdateBlogRequest;
import com.nytd.demo.pojo.response.BlogDetailResponse;
import com.nytd.demo.pojo.response.BlogResponse;
import com.nytd.demo.service.BlogInfoService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service("blogInfoService")
public class BlogInfoServiceImpl implements BlogInfoService {
    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogResponse> getList() {
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getDeleteFlag, Constant.NOT_DELETE)
                .orderByAsc(BlogInfo::getId));
        List<BlogResponse> blogResponses = blogInfos.stream().map(blogInfo -> {
            BlogResponse response = new BlogResponse();
            BeanUtils.copyProperties(blogInfo, response);
            return response;
        }).collect(Collectors.toList());
        return blogResponses;
    }

    @Override
    public BlogDetailResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getDeleteFlag, Constant.NOT_DELETE)
                .eq(BlogInfo::getId, blogId));
        if (blogInfo == null) {
            throw new BlogException("博客不存在");
        }
        BlogDetailResponse blogDetailResponse = new BlogDetailResponse();
        BeanUtils.copyProperties(blogInfo, blogDetailResponse);
        return blogDetailResponse;
    }

    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest, blogInfo);
        Integer result = null;
        try {
            result = insertBlog(blogInfo);
            if (result == 1) {
                return true;
            }
        } catch (BlogException e) {
            throw new BlogException("新增博客失败");
        }
        return false;
    }

    @Override
    public Boolean update(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(updateBlogRequest, blogInfo);
        try {
            Integer result = updateBlogById(blogInfo);
            if (result == 1) {
                return true;
            }
        } catch (BlogException e) {
            throw new BlogException("编辑失败");
        }
        return false;
    }

    @Override
    public Boolean delete(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constant.IS_DELETE);
        try {
            Integer result = updateBlogById(blogInfo);
            if (result == 1) {
                return true;
            }
        } catch (BlogException e) {
            throw new BlogException("删除失败");
        }
        return false;
    }

    public Integer updateBlogById(BlogInfo blogInfo) {
        return blogInfoMapper.updateById(blogInfo);
    }

    public Integer insertBlog(BlogInfo blogInfo) {
        return blogInfoMapper.insert(blogInfo);
    }
}
